//
//  ZQWebViewController.m
//  BuDeJie
//
//  Created by 仲琦 on 16/4/8.
//  Copyright © 2016年 仲琦. All rights reserved.
//

#import "ZQWebViewController.h"
#import <WebKit/WebKit.h>

@interface ZQWebViewController ()
@property (weak, nonatomic) IBOutlet UIProgressView *progressV;

@property (nonatomic ,strong)WKWebView *webview;

@end

@implementation ZQWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    //创建 webView
    WKWebView *webview = [[WKWebView alloc]initWithFrame:self.view.frame];
    [self.view insertSubview:webview atIndex:0];
    
    //设置网址
    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:_url];
    
    _webview = webview;
    
    //添加到webView
    [webview loadRequest:request];
    
    
      // KVO: 让self对象监听webView的estimatedProgress
    [webview addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];

    
}


//kvo调用方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    
    _progressV.progress = _webview.estimatedProgress;
    
    _progressV.hidden = _progressV.progress >= 1 ;
    
}


//KVO使用完成一定要移除观察者
- (void)dealloc
{
    [self.webview removeObserver:self forKeyPath:@"estimatedProgress"];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
